package com.cmc.cloud.cmclink.doc.controller.admin.approvespace;

import cn.hutool.core.bean.BeanUtil;
import com.cmc.cloud.cmclink.doc.constants.CommonConstants;
import com.cmc.cloud.cmclink.doc.constants.UrlConstants;
import com.cmc.cloud.cmclink.doc.entity.RejectReasonDO;
import com.cmc.cloud.cmclink.doc.service.RejectReasonService;
import com.cmc.cloud.cmclink.doc.service.approvespace.ApproveSpaceService;
import com.cmc.cloud.cmclink.doc.vo.rejectreasonvo.RejectReasonRespVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.approvespacevo.SoApproveSpaceReqVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.approvespacevo.SoApproveSpaceResVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.approvespacevo.SoRejectSpaceReqVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.approvespacevo.UpdateBkgVesselReqVO;
import com.cmc.cloud.cmclink.doc.vo.sovo.sovo.SoDetailRespVO;
import com.cmes.framework.common.pojo.CommonResult;
import com.cmes.framework.common.pojo.PageResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * @author lzp
 */
@Tag(description = "后台管理-批舱", name = "后台管理-批舱")
@RestController
@RequestMapping(UrlConstants.APPLICATION_URL + "/approveSpace")
public class ApproveSpaceController {

    @Resource
    private ApproveSpaceService approveSpaceService;

    @Resource
    private RejectReasonService rejectReasonService;

    @PostMapping("/search")
    @Operation(description = "批舱信息查询", summary = "批舱信息查询")
    @PreAuthorize("@ss.hasPermission('doc:approve-space:search')")
    public CommonResult<PageResult<SoApproveSpaceResVO>>
        searchSoApproveSpacePage(@RequestBody SoApproveSpaceReqVO soApproveSpaceReqVO) {
        PageResult<SoApproveSpaceResVO> result = approveSpaceService.searchSoApproveSpacePage(soApproveSpaceReqVO);
        return CommonResult.success(result);
    }

    @Operation(summary = "获得订舱详细")
    @GetMapping("/get/{id}")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('doc:approve-space:document')")
    public CommonResult<SoDetailRespVO> getSo(@PathVariable("id") Long id) {
        return CommonResult.success(approveSpaceService.getDetail(id));
    }

    @PostMapping("/update/bkg")
    @Operation(description = "修改BKG", summary = "修改BKG")
    @PreAuthorize("@ss.hasPermission('doc:approve-space:BKG')")
    public CommonResult<Void> updateBkgVessel(@RequestBody UpdateBkgVesselReqVO updateBkgVesselReqVO) {
        approveSpaceService.updateBkgVessel(updateBkgVesselReqVO);
        return CommonResult.success();
    }

    @Operation(summary = "同意批舱")
    @PostMapping("/approve/{id}")
    @Parameter(name = "id", description = "船名航次id", required = true, example = "1024")
    public CommonResult<String> approveSpace(@PathVariable("id") Long id) {
        approveSpaceService.approveSpace(id);
        return CommonResult.success(CommonConstants.APPROVE_SUCCESS);
    }

    @Operation(summary = "拒绝批舱")
    @PostMapping("/reject/{id}")
    @Parameter(name = "id", description = "船名航次id", required = true, example = "1024")
    public CommonResult<String> rejectSpace(@PathVariable("id") Long id,
        @Valid @RequestBody SoRejectSpaceReqVO soRejectSpaceReqVO) {
        approveSpaceService.rejectSpace(id, soRejectSpaceReqVO);
        return CommonResult.success(CommonConstants.REJECT_SUCCESS);
    }

    @Operation(summary = "舱位待定")
    @PostMapping("/pending/{id}")
    @Parameter(name = "id", description = "船名航次id", required = true, example = "1024")
    public CommonResult<String> pendingSpace(@PathVariable("id") Long id,
        @Valid @RequestBody SoRejectSpaceReqVO soRejectSpaceReqVO) {
        approveSpaceService.pendingSpace(id, soRejectSpaceReqVO);
        return CommonResult.success(CommonConstants.PENDING_SUCCESS);
    }

    @Operation(summary = "获得批舱拒绝理由列表")
    @GetMapping("/reason/listAll")
    @Parameter(name = "enable", description = "启用标志", example = "true, false")
    public CommonResult<List<RejectReasonRespVO>>
        getRejectReasonList(@RequestParam(value = "enable", required = false) Boolean enable) {
        List<RejectReasonDO> list = rejectReasonService.getRejectReasonEnableList(enable);
        return CommonResult.success(BeanUtil.copyToList(list, RejectReasonRespVO.class));
    }
}
